<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>路由操作 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/102.c7d7ed9c.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/95.1b379a5b.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go Web框架</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/web/iris-go/start.html" class="sidebar-link">Iris概览</a></li><li><a href="/go/web/iris-go/config.html" class="sidebar-link">配置信息</a></li><li><a href="/go/web/iris-go/handle-http.html" class="sidebar-link">处理HTTP请求</a></li><li><a href="/go/web/iris-go/route.html" class="active sidebar-link">路由操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_1-基本介绍" class="sidebar-link">1 基本介绍</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_2-context概念" class="sidebar-link">2 Context概念</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_3-普通路由" class="sidebar-link">3 普通路由</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_4-动态路由" class="sidebar-link">4 动态路由</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_4-1-举例" class="sidebar-link">4.1 举例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_5-路由分组" class="sidebar-link">5 路由分组</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_5-1-路由组示例" class="sidebar-link">5.1 路由组示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_6-路由中间件" class="sidebar-link">6 路由中间件</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_6-1-简单示例" class="sidebar-link">6.1 简单示例</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_6-2-全局中间件" class="sidebar-link">6.2 全局中间件</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_6-3-相关源码学习" class="sidebar-link">6.3 相关源码学习</a></li><li class="sidebar-sub-header"><a href="/go/web/iris-go/route.html#_6-4-常用中间件" class="sidebar-link">6.4 常用中间件</a></li></ul></li></ul></li><li><a href="/go/web/iris-go/error-handling.html" class="sidebar-link">错误处理</a></li><li><a href="/go/web/iris-go/mvc.html" class="sidebar-link">MVC包的使用</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go工具中间件</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="路由操作"><a href="#路由操作" class="header-anchor">#</a> 路由操作</h1> <h2 id="_1-基本介绍"><a href="#_1-基本介绍" class="header-anchor">#</a> 1 基本介绍</h2> <blockquote><p>所有的<code>HTTP</code>方法都支持,开发者可以为相同路径的处理程序注册不同的方法。第一个参数是<code>HTTP</code>方法,
第二个参数是路径的请求路径, 第三个可变参数应该包含一个或多个<code>iris.Handler</code>, 当用户从服务器请求该特定的资源路径时,
由注册的顺序执行。示例代码如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//请求方法类型:GET</span>
	<span class="token comment">//请求路径:localhost:8080/contact</span>
	<span class="token comment">//处理请求方法的函数:func(ctx iris.Context)</span>
	app<span class="token punctuation">.</span><span class="token function">Handle</span><span class="token punctuation">(</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/contact&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		ctx<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt; Hello World &lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//8080 监听端口 ，如果未发现服务则返回系统自定义的错误</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8080&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithoutServerError</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>ErrServerClosed<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_2-context概念"><a href="#_2-context概念" class="header-anchor">#</a> 2 Context概念</h2> <blockquote><p><code>Context</code>是<code>iris</code>框架中的一个路由上下文对象,在<code>iris</code>框架中的源码路径定义为：<code>{$goPath}/pkg/mod/github.com/kataras/iris/v12@v12.1.3/context/context.go</code>。<a href="https://github.com/kataras/iris/tree/master/_examples" target="_blank" rel="noopener noreferrer">源码示例参考<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>以下是Context的声明和定义：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> context
<span class="token keyword">type</span> Context <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// BeginRequest 针对每一个请求都会执行</span>
    <span class="token comment">// 它应该为新的请求准备(新的或从pool获得的)上下文的字段。</span>

    <span class="token comment">// 要跟随iris的流程，开发人员应:</span>
    <span class="token comment">// 1. 重置handler 为nil</span>
    <span class="token comment">// 2. 重置 values 为空</span>
    <span class="token comment">// 3. 重置会话为 nil</span>
    <span class="token comment">// 4. 重置 response writer 到 http.ResponseWriter</span>
    <span class="token comment">// 5. 重置 request 到 *http.Request</span>
    <span class="token comment">// 任何其他可选步骤都取决于开发的应用程序类型。</span>
    <span class="token function">BeginRequest</span><span class="token punctuation">(</span>http<span class="token punctuation">.</span>ResponseWriter<span class="token punctuation">,</span> <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span>

    <span class="token comment">// 在请求被响应以后 执行EndRequest，并且这个请求的上下文变为无效或者已经释放</span>
    <span class="token comment">// 要跟随iris的流程，开发人员应:</span>
    <span class="token comment">// 1.刷新响应编写器的结果</span>
    <span class="token comment">// 2.刷新响应编写器的结果</span>
    <span class="token comment">// 任何其他可选步骤都取决于开发的应用程序类型。</span>
    <span class="token function">EndRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">//ResponseWriter按预期返回与http.ResponseWriter兼容的响应编写器。</span>
    <span class="token function">ResponseWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> ResponseWriter

    <span class="token comment">// 改变或者升级ResponseWriter.</span>
    <span class="token function">ResetResponseWriter</span><span class="token punctuation">(</span>ResponseWriter<span class="token punctuation">)</span>

    <span class="token comment">// 按预期返回原始的* http.Request。</span>
    <span class="token function">Request</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span>http<span class="token punctuation">.</span>Request

    <span class="token comment">// SetCurrentRouteName在内部设置路由的名称，</span>
    <span class="token comment">//为了能够找到正确的当前“只读”路由时</span>
    <span class="token comment">// end-developer调用`GetCurrentRoute（）`函数。</span>
    <span class="token comment">//如果您更改了该名称，它将由路由器初始化,他只会更改当名称</span>
    <span class="token comment">// 通过`GetCurrentRoute（）`您将会得到所更改的名称。</span>

    <span class="token comment">//相反，要执行不同的路径</span>
    <span class="token comment">//你应该使用`Exec`函数</span>
    <span class="token comment">//或通过`SetHandlers 或者 AddHandler`函数更改处理程序。</span>
    <span class="token function">SetCurrentRouteName</span><span class="token punctuation">(</span>currentRouteName <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// GetCurrentRoute返回当前注册的“只读”路由</span>
    <span class="token comment">//正在注册此请求的路径。</span>
    <span class="token comment">//r:=ctx.GetCurrentRoute()//获取当前路由</span>
    <span class="token comment">//fmt.Println(r)//输出:GET /users/{id:int}/profile</span>
    <span class="token function">GetCurrentRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span> RouteReadOnly

    <span class="token comment">//调用  SetHandlers(handlers)</span>
    <span class="token comment">// 并且执行该方法所设置的第一个handlers</span>
    <span class="token comment">// handlers 不应该为空.</span>
    <span class="token comment">// 这是路由器使用的 开发者应该使用exex(Handler)直接调用要执行的handler</span>
    <span class="token function">Do</span><span class="token punctuation">(</span>Handlers<span class="token punctuation">)</span>

    <span class="token comment">// AddHandler可以添加处理程序</span>
    <span class="token comment">//到服务时间的当前请求，</span>
    <span class="token comment">//这些处理程序没有持久化到路由器。</span>
    <span class="token comment">//路由器正在调用此函数来添加路由的处理程序。</span>
    <span class="token comment">//如果调用了AddHandler，则会插入处理程序</span>
    <span class="token comment">//到已经定义的路由处理程序的末尾。</span>
    <span class="token comment">//ctx.Handlers()[1](ctx)//调用当前添加的函数1为当前handler再handlers数组中的下标</span>
    <span class="token comment">//再当前被添加的函数中使用ctx.HandlerIndex(2)定义该函数的位置</span>
    <span class="token function">AddHandler</span><span class="token punctuation">(</span><span class="token operator">...</span>Handler<span class="token punctuation">)</span>

    <span class="token comment">//SetHandlers用新的 handler取代所有处理程序。</span>
    <span class="token function">SetHandlers</span><span class="token punctuation">(</span>Handlers<span class="token punctuation">)</span>

    <span class="token comment">//返回当前所有的可调用的handler。</span>
    <span class="token function">Handlers</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Handlers

    <span class="token comment">// 设置当前hander 在handeler数组中的下标位置返回-1代表ok</span>
    <span class="token comment">// Look Handlers(), Next() and StopExecution() too.</span>
    <span class="token function">HandlerIndex</span><span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>currentIndex <span class="token builtin">int</span><span class="token punctuation">)</span>

    <span class="token comment">// Proceed 是检查特定处理程序是否已被执行并在其中调用`ctx.Next`函数的另一种方法</span>
    <span class="token comment">// 只有在内部运行处理程序时，这才有用</span>
    <span class="token comment">// 另一个处理它只是在索引和after索引之前检查。</span>

    <span class="token comment">// 一个使用实例就是去执行一个中间件</span>
    <span class="token comment">// 在控制器的`BeginRequest`中调用其中的`ctx.Next`。</span>
    <span class="token comment">// Controller查看整个流程（BeginRequest，方法处理程序，EndRequest） 作为一个处理程序，</span>
    <span class="token comment">// 所以`ctx.Next`不会反映到方法处理程序中</span>
    <span class="token comment">// 如果从`BeginRequest`调用。</span>
    <span class="token comment">//</span>
    <span class="token comment">// 虽然`BeginRequest`不应该用于调用其他处理程序，</span>
    <span class="token comment">//引入了`BeginRequest`以便能够设置 执行前所有方法处理程序的公共数据。</span>
    <span class="token comment">// 控制器可以正常接受来自MVC的应用程序路由器的中间件。</span>
    <span class="token comment">// 让我们看一个`ctx.Proceed`的例子：</span>
    <span class="token comment">// var authMiddleware = basicauth.New(basicauth.Config{</span>
    <span class="token comment">//         Users: map[string]string{</span>
    <span class="token comment">//             &quot;admin&quot;: &quot;password&quot;,</span>
    <span class="token comment">//         },</span>
    <span class="token comment">// })</span>
    <span class="token comment">// func (c *UsersController) BeginRequest(ctx iris.Context) {</span>
    <span class="token comment">//     if !ctx.Proceed(authMiddleware) {</span>
    <span class="token comment">//         ctx.StopExecution()</span>
    <span class="token comment">//      }</span>
    <span class="token comment">// }</span>
    <span class="token comment">//这个Get（）将在与`BeginRequest`相同的处理程序中执行，</span>
    <span class="token comment">//内部控制器检查`ctx.StopExecution`。</span>
    <span class="token comment">//因此，如果BeginRequest调用了`StopExecution`，它将不会被触发。</span>
    <span class="token comment">// func(c *UsersController) Get() []models.User {</span>
    <span class="token comment">//      return c.Service.GetAll()</span>
    <span class="token comment">// }</span>
    <span class="token comment">// 另一种方法是`！ctx.IsStopped（）`如果中间件在失败时使用`ctx.StopExecution（）`。</span>
    <span class="token function">Proceed</span><span class="token punctuation">(</span>Handler<span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// HandlerName返回当前处理程序的名称，有助于调试。格式package.function</span>
    <span class="token function">HandlerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token operator">/</span> Next从处理程序链中调用所有下一个处理程序<span class="token punctuation">,</span>
    <span class="token comment">//它应该在中间件中使用</span>
    <span class="token comment">//注意：自定义上下文应该重写此方法，以便能够传递自己的context.Context实现。</span>
    <span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">// NextOr检查链是否有下一个处理程序，如果是，则执行它</span>
    <span class="token comment">//否则它根据给定的处理程序设置分配给此Context的新链</span>
    <span class="token comment">//并执行其第一个处理程序。</span>

    <span class="token comment">//如果存在并执行下一个处理程序，则返回true，否则返回false</span>
    <span class="token comment">//请注意，如果找不到下一个处理程序，则缺少处理程序</span>
    <span class="token comment">//它向客户端发送未找到状态（404）并停止执行。</span>
    <span class="token function">NextOr</span><span class="token punctuation">(</span>handlers <span class="token operator">...</span>Handler<span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// NextOrNotFound检查链是否有下一个处理程序，如果是，则执行它</span>
    <span class="token comment">//否则它会向客户端发送未找到状态（404）并停止执行。</span>
    <span class="token comment">//如果存在并执行下一个处理程序，则返回true，否则返回false</span>
    <span class="token function">NextOrNotFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// NextHandler从处理程序链返回（它不执行）下一个处理程序。</span>
    <span class="token comment">//如果需要执行下一个返回处理程序，请使用.Skip（）跳过此处理程序。</span>
    <span class="token function">NextHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Handler
    <span class="token comment">//跳过/忽略处理程序链中的下一个处理程序，</span>
    <span class="token comment">//它应该在中间件中使用</span>
    <span class="token function">Skip</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token comment">//终止当前程序但不是退出（等于设定一个终止标记）</span>
    <span class="token comment">//如果调用StopExecution，则调用以下.Next调用被忽略，</span>
    <span class="token comment">//因此链中的下一个处理程序不会被触发。</span>
    <span class="token function">StopExecution</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// IsStopped检查并在Context的当前位置为255时返回true，</span>
    <span class="token comment">//表示调用了StopExecution（）</span>
    <span class="token function">IsStopped</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// 请求参数介绍</span>
    <span class="token comment">// Params返回当前url的命名参数键值存储。</span>
    <span class="token comment">//这里保存了命名路径参数。</span>
    <span class="token comment">//作为整个Context，此存储是按请求生存期。</span>
    <span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span>RequestParams

    <span class="token operator">/</span>值返回当前的“用户”存储。
    <span class="token comment">//可以在此处保存命名路径参数和任何可选数据。</span>
    <span class="token comment">//作为整个Context，此存储是按请求生存期。</span>
    <span class="token comment">//您可以使用此函数来设置和获取本地值</span>
    <span class="token comment">//可用于在处理程序和中间件之间共享信息。</span>
    <span class="token function">Values</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span>memstore<span class="token punctuation">.</span>Store

    <span class="token comment">//翻译是i18n（本地化）中间件的功能，</span>
    <span class="token comment">//它调用Get（“translate”）来返回翻译的值。</span>
    <span class="token comment">//示例：https：//github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n</span>
    <span class="token function">Translate</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  |路径,主机,子域,IP,标头等...              |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// 返回request.Method，客户端的http方法返回给服务器</span>
    <span class="token function">Method</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// Path返回完整的请求路径，/name 不包含host 和请求参数</span>
   <span class="token comment">//如果EnablePathEscape配置字段为true，则进行转义。 </span>
    <span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// RequestPath返回完整的请求路径</span>
    <span class="token comment">//基于'escape'。</span>
    <span class="token function">RequestPath</span><span class="token punctuation">(</span>escape <span class="token builtin">bool</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// Host返回当前url的主机部分。如localhost</span>
    <span class="token function">Host</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// Subdomain返回此请求的子域（如果有）。</span>
    <span class="token comment">//请注意，这是一种不能涵盖所有情况的快速方法。</span>
    <span class="token function">Subdomain</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>subdomain <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">//如果当前子域（如果有）是www，则IsWWW返回true。</span>
    <span class="token function">IsWWW</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token operator">/</span> RemoteAddr尝试解析并返回真实客户端的请求IP。
    <span class="token comment">//基于可从Configuration.RemoteAddrHeaders修改的允许标头名称。</span>
    <span class="token comment">//如果基于这些头的解析失败，那么它将返回Request的`RemoteAddr`字段</span>
    <span class="token comment">//在HTTP处理程序之前由服务器填充。</span>

    <span class="token comment">//查看  `Configuration.WithRemoteAddrHeader(...)`,</span>
    <span class="token comment">//      `Configuration.WithoutRemoteAddrHeader(...)` for more.</span>
    <span class="token function">RemoteAddr</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// GetHeader根据名称返回请求标头的值。</span>
    <span class="token function">GetHeader</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">//如果此请求是'ajax请求'（XMLHttpRequest），则IsAjax返回true</span>
    <span class="token comment">//没有100％的方式知道请求是通过Ajax进行的。</span>
    <span class="token comment">//您永远不应该信任来自客户端的数据，可以通过欺骗轻松克服这些数据。</span>

    <span class="token comment">//注意“X-Requested-With”标题可以被任何客户端修改（因为“X-”），</span>
    <span class="token comment">//所以不要依赖IsAjax来做真正严肃的事情，</span>
    <span class="token comment">//尝试找到另一种检测类型的方法（即内容类型），</span>
    <span class="token comment">//有很多博客描述这些问题并提供不同类型的解决方案，</span>
    <span class="token comment">//它始终取决于您正在构建的应用程序，</span>
    <span class="token comment">//这就是为什么这个'IsAjax``足够简单以便通用的原因。</span>
    <span class="token comment">//阅读更多信息：</span>
    <span class="token function">IsAjax</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// IsMobile会检查客户端是否正在使用移动设备（手机或平板电脑）与此服务器通信。</span>
    <span class="token comment">//如果返回值为true，则表示http客户端使用移动设备</span>
    <span class="token comment">//设备与服务器通信，否则为false。</span>
    <span class="token comment">//请注意，这会检查“User-Agent”请求标头。</span>
    <span class="token function">IsMobile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  |响应头助手                                   |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// 添加一个响应头</span>
    <span class="token function">Header</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">//设置响应头 &quot;Content-Type&quot; to the 'cType'.</span>
    <span class="token function">ContentType</span><span class="token punctuation">(</span>cType <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// GetContentType返会响应头 “Content-Type”</span>
    <span class="token comment">//可以使用'ContentType'设置之前。</span>
    <span class="token function">GetContentType</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// GetContentLength 返回请求头 &quot;Content-Length&quot;. 的值</span>
    <span class="token comment">//当改值没有找到或者不是一个数字 将会返回0</span>
    <span class="token function">GetContentLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int64</span>

    <span class="token comment">// StatusCode 设置响应的statu code 值</span>

    <span class="token function">StatusCode</span><span class="token punctuation">(</span>statusCode <span class="token builtin">int</span><span class="token punctuation">)</span>

    <span class="token comment">//返回响应的当前状态代码。</span>
    <span class="token function">GetStatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>

    <span class="token comment">//重定向向客户端发送重定向响应</span>
    <span class="token comment">//到特定网址或相对路径。</span>
    <span class="token comment">//接受2个参数字符串和一个可选的int</span>
    <span class="token comment">//第一个参数是要重定向的URL</span>
    <span class="token comment">//第二个参数是应该发送的http状态，</span>
    <span class="token comment">//默认为302（StatusFound），</span>
    <span class="token comment">//你可以将它设置为301（Permant重定向）</span>
    <span class="token comment">//或303（StatusSeeOther）如果POST方法，</span>
    <span class="token comment">//或StatusTemporaryRedirect（307），如果这是nessecery。</span>
    <span class="token function">Redirect</span><span class="token punctuation">(</span>urlToRedirect <span class="token builtin">string</span><span class="token punctuation">,</span> statusHeader <span class="token operator">...</span><span class="token builtin">int</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | 各种请求和Post数据                            |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// URLParam 如果url参数存在，则返回true，否则返回false。</span>
    <span class="token function">URLParamExists</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// URLParamDefault返回请求中的get参数，</span>
    <span class="token comment">//如果没有找到 则返回 设定的 &quot;def&quot;.</span>
    <span class="token function">URLParamDefault</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">//URLParam返回请求中的get参数（如果有）。</span>
    <span class="token function">URLParam</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// 返回url查询参数，其中从请求中删除了尾随空格。</span>
    <span class="token function">URLParamTrim</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// URLParamTrim 从请求中返回转义的url查询参数。URLParamEscape（名称字符串）字符串</span>
    <span class="token function">URLParamEscape</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// URLParamInt将url查询参数作为来自请求的int值返回，</span>
    <span class="token comment">//返回-1，如果解析失败则返回错误。</span>
    <span class="token function">URLParamInt</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">//将url查询参数作为来自请求的int值返回，</span>
    <span class="token comment">//如果找不到或解析失败，则返回 设定的 &quot;def&quot;.</span>
    <span class="token function">URLParamIntDefault</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span>

    <span class="token comment">// URLParamInt64 将url查询参数作为来自请求的int64值返回,</span>
    <span class="token comment">//返回-1，如果解析失败则返回错误。</span>
    <span class="token function">URLParamInt64</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int64</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// URLParamInt64Default  将url查询参数作为来自请求的int64值返回,</span>
    <span class="token comment">//如果找不到或解析失败，则返回 设定的 &quot;def&quot;.</span>
    <span class="token function">URLParamInt64Default</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token builtin">int64</span>

    <span class="token comment">// URLParamFloat64 将url查询参数作为来自请求的float64值返回,</span>
    <span class="token comment">//返回-1，如果解析失败则返回错误。</span>
    <span class="token function">URLParamFloat64</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">float64</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// URLParamFloat64Default  将url查询参数作为来自请求的float64值返回,</span>
    <span class="token comment">//如果找不到或解析失败，则返回 设定的 &quot;def&quot;.</span>
    <span class="token function">URLParamFloat64Default</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">float64</span><span class="token punctuation">)</span> <span class="token builtin">float64</span>
    <span class="token comment">// URLParamFloat64 将url查询参数作为来自请求的bool值返回,</span>
    <span class="token comment">//返回-1，如果解析失败则返回错误。</span>

    <span class="token function">URLParamBool</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">bool</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// URLParams返回由逗号分隔的GET查询参数的映射（如果有多个）</span>
    <span class="token comment">//如果找不到任何内容，则返回空map。</span>

    <span class="token function">URLParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span>

    <span class="token comment">// FormValueDefault通过其&quot;名称&quot;返回单个解析的表单值,</span>
    <span class="token comment">//包括URL字段的查询参数和POST或PUT表单数据。</span>
    <span class="token comment">//如果没有找到返回设定的def</span>
    <span class="token function">FormValueDefault</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// FormValue通过其“名称”返回单个解析的表单值，</span>
    <span class="token comment">//包括URL字段的查询参数和POST或PUT表单数据。</span>
    <span class="token function">FormValue</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// FormValues返回已解析的表单数据，包括URL</span>
    <span class="token comment">//字段的查询参数和POST或PUT表单数据。</span>

    <span class="token comment">//默认表单的内存最大大小为32MB，可以通过更改</span>
    <span class="token comment">//在主要配置中的`iris＃WithPostMaxMemory`配置器传递给`app.Run`的第二个参数。</span>

    <span class="token comment">//注意：需要检查nil。</span>
    <span class="token function">FormValues</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>

    <span class="token comment">// PostValueDefault从POST，PATCH返回解析后的表单数据</span>
    <span class="token comment">//或基于“名称”的PUT身体参数。</span>
   <span class="token comment">//如果没有找到返回设定的def</span>
    <span class="token comment">// If not found then &quot;def&quot; is returned instead.</span>
    <span class="token function">PostValueDefault</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// PostValue从POST，PATCH返回解析后的表单数据</span>
    <span class="token comment">//或基于“名称”的PUT身体参数</span>
    <span class="token function">PostValue</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// PostValueTrim从POST，PATCH返回解析后的表单数据</span>
    <span class="token comment">//或PUT基于“名称”的体参数，没有尾随空格。</span>
    <span class="token function">PostValueTrim</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>

    <span class="token comment">// PostValueInt从POST，PATCH返回解析后的表单数据</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如int。</span>

    <span class="token comment">//如果未找到则返回-1并返回非nil错误。</span>
    <span class="token function">PostValueInt</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// PostValueIntDefault从POST，PATCH返回解析后的表单数据</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如int</span>

    <span class="token comment">//如果没有找到返回设定的def</span>
    <span class="token function">PostValueIntDefault</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span>

    <span class="token comment">// PostValueInt64从POST，PATCH返回解析后的表单数据，</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如float64。</span>
    <span class="token comment">// 如果未找到则返回-1并返回非nil错误。</span>
     <span class="token function">PostValueInt64</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int64</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// PostValueInt64Default从POST，PATCH返回解析后的表单数据，</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如int64。</span>
    <span class="token comment">//如果没有找到返回设定的def</span>
    <span class="token function">PostValueInt64Default</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">int64</span><span class="token punctuation">)</span> <span class="token builtin">int64</span>

    <span class="token comment">// PostValueFloat64从POST，PATCH返回解析后的表单数据，</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如float64。</span>
    <span class="token comment">// 如果未找到则返回-1并返回非nil错误。</span>
    <span class="token function">PostValueFloat64</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">float64</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// PostValueInt64Default从POST，PATCH返回解析后的表单数据，</span>
    <span class="token comment">//或PUT基于“名称”的体参数，如Int64。</span>
    <span class="token comment">//如果没有找到返回设定的def</span>
    <span class="token function">PostValueFloat64Default</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> def <span class="token builtin">float64</span><span class="token punctuation">)</span> <span class="token builtin">float64</span>

    <span class="token comment">// PostValueInt64Default从POST，PATCH返回解析后的表单数据，</span>
    <span class="token comment">//或PUT基于“名称”的身体参数，如bool。</span>

    <span class="token comment">//如果未找到或value为false，则返回false，否则返回true。</span>
    <span class="token function">PostValueBool</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">bool</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// PostValues从POST，PATCH返回所有已解析的表单数据，</span>
    <span class="token comment">//或PUT体参数基于“名称”作为字符串切片。</span>
    <span class="token comment">//</span>
    <span class="token comment">//默认表单的内存最大大小为32MB，可以通过更改</span>
    <span class="token comment">//在主要配置中的`iris＃WithPostMaxMemory`配置器传递给`app.Run`的第二个参数。</span>

    <span class="token function">PostValues</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>

    <span class="token comment">// FormFile返回从客户端收到的第一个上传文件。</span>
    <span class="token comment">//默认表单的内存最大大小为32MB，可以通过更改</span>
    <span class="token comment">//在主要配置中的`iris＃WithPostMaxMemory`配置器传递给`app.Run`的第二个参数。</span>
    <span class="token comment">//示例：https：//github.com/kataras/iris/tree/master/_examples/http_request/upload-file</span>

    <span class="token function">FormFile</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>multipart<span class="token punctuation">.</span>File<span class="token punctuation">,</span> <span class="token operator">*</span>multipart<span class="token punctuation">.</span>FileHeader<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// UploadFormFiles从客户端上传任何收到的文件</span>
    <span class="token comment">//到系统物理位置“destDirectory”。</span>

    <span class="token comment">//第二个可选参数“before”为调用者提供了机会</span>
    <span class="token comment">//在保存到磁盘之前修改* miltipart.FileHeader，</span>
    <span class="token comment">//它可以用来根据当前请求更改文件名，</span>
    <span class="token comment">//可以更改所有FileHeader的选项。你可以忽略它</span>
    <span class="token comment">//在将文件保存到磁盘之前，您不需要使用此功能。</span>
    <span class="token comment">//请注意，它不会检查请求正文是否已流式传输。</span>

    <span class="token comment">//将复制的长度返回为int64和</span>
    <span class="token comment">//如果至少有一个新文件，则为非nil错误</span>
    <span class="token comment">//由于操作系统的权限或无法创建//</span>
    <span class="token comment">// http.ErrMissingFile如果没有收到文件。</span>

    <span class="token comment">//如果您想要接收和接受文件并手动管理它们，您可以使用`context＃FormFile`</span>
    <span class="token comment">//而是创建一个适合您需要的复制功能，下面是一般用法。</span>

    <span class="token comment">//默认表单的内存最大大小为32MB，可以通过更改</span>
    <span class="token comment">//在主要配置中的`iris＃WithPostMaxMemory`配置器传递给`app.Run`的第二个参数。</span>

    <span class="token comment">//参见`FormFile`更受控制以接收文件。</span>
    <span class="token comment">//示例：https：//github.com/kataras/iris/tree/master/_examples/http_request/upload-files</span>
    <span class="token function">UploadFormFiles</span><span class="token punctuation">(</span>destDirectory <span class="token builtin">string</span><span class="token punctuation">,</span> before <span class="token operator">...</span><span class="token keyword">func</span><span class="token punctuation">(</span>Context<span class="token punctuation">,</span> <span class="token operator">*</span>multipart<span class="token punctuation">.</span>FileHeader<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>n <span class="token builtin">int64</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | 自定义HTTP错误                                               |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// NotFound使用特定的自定义错误错误处理程序向客户端发出错误404。</span>
    <span class="token comment">//请注意，如果您不想使用下一个处理程序，则可能需要调用ctx.StopExecution（）</span>
    <span class="token comment">//被执行下一个处理程序正在iris上执行，因为你可以使用它</span>
    <span class="token comment">//错误代码并将其更改为更具体的错误代码，即</span>
    <span class="token comment">// users：= app.Party（“/ users”）</span>
    <span class="token comment">// users.Done（func（ctx context.Context）{if ctx.StatusCode（）== 400 {/ * / users * /}的自定义错误代码}）</span>
    <span class="token function">NotFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Body Readers                                               |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// SetMaxRequestBodySize设置请求主体大小的限制</span>
    <span class="token comment">//应该在从客户端读取请求主体之前调用。</span>
    <span class="token function">SetMaxRequestBodySize</span><span class="token punctuation">(</span>limitOverBytes <span class="token builtin">int64</span><span class="token punctuation">)</span>

    <span class="token comment">// UnmarshalBody读取请求的正文并将其绑定到任何类型的值或指针。</span>
    <span class="token comment">//用法示例：context.ReadJSON，context.ReadXML。</span>

    <span class="token comment">//例如: https://github.com/kataras/iris/blob/master/_examples/http_request/read-custom-via-unmarshaler/main.go</span>
    <span class="token function">UnmarshalBody</span><span class="token punctuation">(</span>outPtr <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> unmarshaler Unmarshaler<span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token comment">// ReadJSON从请求的主体读取JSON，并将其绑定到任何json-valid类型的值的指针。</span>

    <span class="token comment">// 例如: https://github.com/kataras/iris/blob/master/_examples/http_request/read-json/main.go</span>
    <span class="token function">ReadJSON</span><span class="token punctuation">(</span>jsonObjectPtr <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token comment">// ReadXML从请求的正文中读取XML，并将其绑定到任何xml-valid类型值的指针。</span>

    <span class="token comment">// 例如: https://github.com/kataras/iris/blob/master/_examples/http_request/read-xml/main.go</span>
    <span class="token function">ReadXML</span><span class="token punctuation">(</span>xmlObjectPtr <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span>

    <span class="token comment">// ReadForm将formObject与表单数据绑定在一起</span>
    <span class="token comment">//它支持任何类型的结构。</span>
    <span class="token comment">// Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go</span>
    <span class="token function">ReadForm</span><span class="token punctuation">(</span>formObjectPtr <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Body (raw) Writers                                         |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// Write将数据作为HTTP回复的一部分写入连接。</span>
    <span class="token comment">//如果尚未调用WriteHeader，则写入调用</span>
    <span class="token comment">//在写入数据之前写入WriteHeader（http.StatusOK）。如果是标题</span>
    <span class="token comment">//不包含Content-Type行，Write添加Content-Type集</span>
    <span class="token comment">//将最初的512字节写入数据传递给的结果</span>
    <span class="token comment">// DetectContentType。</span>

    <span class="token comment">//根据HTTP协议版本和客户端，调用</span>
    <span class="token comment">// Write或WriteHeader可能会阻止将来读取</span>
    <span class="token comment">// Request.Body。对于HTTP / 1.x请求，处理程序应该读取任何内容</span>
    <span class="token comment">//在编写响应之前需要请求正文数据。一旦</span>
    <span class="token comment">//标头已被刷新（由于显式的Flusher.Flush</span>
    <span class="token comment">//调用或写入足够的数据来触发刷新），即请求体</span>
    <span class="token comment">//可能无法使用对于HTTP / 2请求，Go HTTP服务器允许</span>
    <span class="token comment">//处理程序在同时继续读取请求体</span>
    <span class="token comment">//写回复但是，可能不支持此类行为</span>
    <span class="token comment">//由所有HTTP / 2客户端。处理者应在写作前阅读</span>
    <span class="token comment">//可以最大化兼容性。</span>
    <span class="token function">Write</span><span class="token punctuation">(</span>body <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// Writef根据格式说明符格式化并写入响应。</span>

    <span class="token comment">//返回写入的字节数和遇到的任何写入错误。</span>
    <span class="token function">Writef</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// WriteString将一个简单的字符串写入响应。</span>

    <span class="token comment">//返回写入的字节数和遇到的任何写入错误。</span>
    <span class="token function">WriteString</span><span class="token punctuation">(</span>body <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// SetLastModified根据“modtime”输入设置“Last-Modified”。</span>
    <span class="token comment">//如果“modtime”为零，那么它什么都不做。</span>

    <span class="token comment">//它主要内部在核心/路由器和上下文包上。</span>
    <span class="token comment">//注意正在使用modtime.UTC（）而不仅仅是modtime，所以</span>
    <span class="token comment">//你不必知道内部结构才能使其有效。</span>
    <span class="token function">SetLastModified</span><span class="token punctuation">(</span>modtime time<span class="token punctuation">.</span>Time<span class="token punctuation">)</span>
    <span class="token comment">// CheckIfModifiedSince检查自“modtime”以来是否修改了响应。</span>
    <span class="token comment">//请注意，它与服务器端缓存无关。</span>
    <span class="token comment">//它通过检查“If-Modified-Since”请求标头来执行这些检查</span>
    <span class="token comment">//由客户端或以前的服务器响应头发送</span>
    <span class="token comment">//（例如WriteWithExpiration或StaticEmbedded或Favicon等）</span>
    <span class="token comment">//是有效的，它在“modtime”之前。</span>

    <span class="token comment">//检查！modtime &amp;&amp; err == nil是必要的，以确保</span>
    <span class="token comment">//它没有被修改，因为它可能会返回false而不是偶数</span>
    <span class="token comment">//由于某些错误，有机会检查客户端（请求）标头，</span>
    <span class="token comment">//喜欢HTTP方法不是“GET”或“HEAD”或者“modtime”是零</span>
    <span class="token comment">//或者从头部解析时间失败。</span>

    <span class="token comment">//它主要用于内部，例如`环境＃WriteWithExpiration`。</span>
    <span class="token comment">//注意正在使用modtime.UTC（）而不仅仅是modtime，所以</span>
    <span class="token comment">//你不必知道内部结构才能使其有效。</span>
    <span class="token function">CheckIfModifiedSince</span><span class="token punctuation">(</span>modtime time<span class="token punctuation">.</span>Time<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">bool</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// WriteNotModified向客户端发送304“未修改”状态代码，</span>
    <span class="token comment">//它确保内容类型，内容长度标题</span>
    <span class="token comment">//在发送响应之前删除任何“ETag”。</span>

    <span class="token comment">//它主要在core / router / fs.go和context方法的内部使用。</span>
    <span class="token function">WriteNotModified</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// WriteWithExpiration类似于Write但它发送的是到期日期时间</span>
    <span class="token comment">//刷新每个包级别的“StaticCacheDuration”字段。</span>
    <span class="token function">WriteWithExpiration</span><span class="token punctuation">(</span>body <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> modtime time<span class="token punctuation">.</span>Time<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// StreamWriter注册给定的流编写器以进行填充</span>
    <span class="token comment">//回复正文</span>

    <span class="token comment">//禁止从作者访问上下文和/或其成员。</span>
    <span class="token comment">//此功能可用于以下情况：</span>

    <span class="token comment">// *如果响应主体太大（超过iris.LimitRequestBodySize（如果已设置））。</span>
    <span class="token comment">// *如果响应正文从缓慢的外部源流式传输。</span>
    <span class="token comment">// *如果必须以块的形式将响应主体流式传输到客户端。</span>
    <span class="token comment">//（又名`http server push`）。</span>

    <span class="token comment">//接收一个接收响应编写器的函数</span>
    <span class="token comment">//并在它应该停止写入时返回false，否则为true以便继续</span>
    <span class="token function">StreamWriter</span><span class="token punctuation">(</span>writer <span class="token keyword">func</span><span class="token punctuation">(</span>w io<span class="token punctuation">.</span>Writer<span class="token punctuation">)</span> <span class="token builtin">bool</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Body Writers with compression                              |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//如果客户端支持gzip压缩，则ClientSupportsGzip重新为true。</span>
    <span class="token function">ClientSupportsGzip</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// WriteGzip接受字节，压缩为gzip格式并发送到客户端。</span>
    <span class="token comment">//返回写入的字节数和错误（如果客户端不支持gzip压缩）</span>
    <span class="token comment">//您可以在同一个处理程序中重用此函数</span>
    <span class="token comment">//多次写入更多数据，没有任何麻烦</span>
    <span class="token function">WriteGzip</span><span class="token punctuation">(</span>b <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// TryWriteGzip接受字节，压缩为gzip格式并发送到客户端。</span>
    <span class="token comment">//如果客户端不支持gzip，则内容按原样写入，未压缩。</span>
    <span class="token function">TryWriteGzip</span><span class="token punctuation">(</span>b <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>

    <span class="token comment">// GzipResponseWriter将当前响应编写器转换为响应编写器</span>
    <span class="token comment">//当它的.Write调用它时，将数据压缩为gzip并将它们写入客户端。</span>
    <span class="token comment">//</span>
    <span class="token comment">//也可以使用.Disable和.ResetBody来禁用它以回滚到通常的响应编写器。</span>
    <span class="token function">GzipResponseWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span>GzipResponseWriter

    <span class="token comment">//如果客户端，Gzip启用或禁用（如果在之前启用）gzip响应编写器</span>
    <span class="token comment">//支持gzip压缩，因此以下响应数据将会</span>
    <span class="token comment">//作为压缩的gzip数据发送到客户端。</span>
    <span class="token function">Gzip</span><span class="token punctuation">(</span>enable <span class="token builtin">bool</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Rich Body Content Writers/Renderers                        |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// ViewLayout在.View时设置“layout”选项</span>
    <span class="token comment">//后来在同一个请求中被调用。</span>
    <span class="token comment">//当需要根据链中的先前处理程序设置或/和更改布局时很有用。</span>
    <span class="token comment">//</span>
    <span class="token comment">//注意'layoutTmplFile'参数可以设置为iris.NoLayout || view.NoLayout</span>
    <span class="token comment">//禁用特定视图渲染操作的布局，</span>
    <span class="token comment">//它会禁用引擎配置的布局属性。</span>

    <span class="token comment">// Look .ViewData and .View too.</span>

    <span class="token comment">// Example: https://github.com/kataras/iris/tree/master/_examples/view/context-view-data/</span>
    <span class="token function">ViewLayout</span><span class="token punctuation">(</span>layoutTmplFile <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// ViewData保存一个或多个键值对，以便在.View时传递</span>
    <span class="token comment">//后来在同一个请求中被调用。</span>
    <span class="token comment">//当需要设置或/和更改链中先前hanadler的模板数据时很有用。</span>

    <span class="token comment">//如果.View的“绑定”参数不是nil而且它不是一种地图</span>
    <span class="token comment">//然后忽略这些数据，绑定具有优先级，因此主路径的处理程序仍然可以决定。</span>
    <span class="token comment">//如果绑定是map或context.Map，那么这些数据将被添加到视图数据中</span>
    <span class="token comment">//并传递给模板。</span>

    <span class="token comment">//在.View之后，数据不会被销毁，以便在需要时重新使用（同样，在与其他所有相同的请求中），</span>
    <span class="token comment">//清除视图数据，开发人员可以调用：</span>
    <span class="token comment">// ctx.Set（ctx.Application（）。ConfigurationReadOnly（）。GetViewDataContextKey（），nil）</span>

    <span class="token comment">//如果'key'为空，则将值添加为（struct或map），并且开发人员无法添加其他值。</span>

    <span class="token comment">// Look .ViewLayout and .View too.</span>
    <span class="token comment">// Example: https://github.com/kataras/iris/tree/master/_examples/view/context-view-data/</span>
    <span class="token function">ViewData</span><span class="token punctuation">(</span>key <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token comment">// GetViewData返回`context #ViewData`注册的值。</span>
    <span class="token comment">//返回值是`map [string] interface {}`，这意味着</span>
    <span class="token comment">//如果一个自定义结构注册到ViewData那么这个函数</span>
    <span class="token comment">//将尝试解析它以进行映射，如果失败则返回值为nil</span>
    <span class="token comment">//如果不同，检查零是一个很好的做法</span>
    <span class="token comment">//通过`ViewData`注册了一些值或没有数据。</span>
    <span class="token comment">//类似于这样 `viewData := ctx.Values().Get(&quot;iris.viewData&quot;)` 或者</span>
    <span class="token comment">//`viewData := ctx.Values().Get(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey())`.</span>
    <span class="token function">GetViewData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token comment">// View根据注册的视图引擎呈现模板。</span>
    <span class="token comment">//第一个参数接受相对于视图引擎的目录和扩展名的文件名，</span>
    <span class="token comment">//即：如果目录是“./templates”并想要呈现“./templates/users/index.html”</span>
    <span class="token comment">//然后你传递“users / index.html”作为文件名参数。</span>

    <span class="token comment">//第二个可选参数可以接收单个“视图模型”</span>
    <span class="token comment">//如果它不是nil，它将绑定到视图模板，</span>
    <span class="token comment">//否则会检查`ViewData`存储的先前视图数据</span>
    <span class="token comment">//即使存储在任何先前的handlerv（中间件）中，也存在相同的请求。</span>

    <span class="token comment">//也看.ViewData`和.ViewLayout。</span>

    <span class="token comment">//示例：https://github.com/kataras/iris/tree/master/_examples/view</span>
    <span class="token function">View</span><span class="token punctuation">(</span>filename <span class="token builtin">string</span><span class="token punctuation">,</span> optionalViewModel <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token builtin">error</span>

    <span class="token comment">// 二进制将原始字节写为二进制数据。</span>
    <span class="token function">Binary</span><span class="token punctuation">(</span>data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// Text将字符串写为纯文本。</span>
    <span class="token function">Text</span><span class="token punctuation">(</span>text <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// HTML将字符串写为 text/html。</span>
    <span class="token function">HTML</span><span class="token punctuation">(</span>htmlContents <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// JSON 整理给定的接口对象并写入JSON响应。</span>
    <span class="token function">JSON</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> options <span class="token operator">...</span>JSON<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// JSONP  整理给定的接口对象并写入JSON响应</span>
    <span class="token function">JSONP</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> options <span class="token operator">...</span>JSONP<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// XML  整理给定的接口对象并写入xml响应.</span>
    <span class="token function">XML</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> options <span class="token operator">...</span>XML<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">//Markdown 将markdown解析为html并将其结果呈现给客户端。</span>
    <span class="token function">Markdown</span><span class="token punctuation">(</span>markdownB <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> options <span class="token operator">...</span>Markdown<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">// 用yaml解析器解析“v”并将其结果呈现给客户端。</span>
    <span class="token function">YAML</span><span class="token punctuation">(</span>v <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Serve files                                                |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// ServeContent提供内容，标题是自动设置的</span>
    <span class="token comment">//接收三个参数，它是低级函数，而不是你可以使用.ServeFile（string，bool）/ SendFile（string，string）</span>

    <span class="token comment">//在此函数调用之前，您可以使用`context＃ContentType`定义自己的“Content-Type”。</span>

    <span class="token comment">//此函数不支持恢复（按范围），</span>
    <span class="token comment">//使用ctx.SendFile或路由器的`StaticWeb`代替。</span>
    <span class="token function">ServeContent</span><span class="token punctuation">(</span>content io<span class="token punctuation">.</span>ReadSeeker<span class="token punctuation">,</span> filename <span class="token builtin">string</span><span class="token punctuation">,</span> modtime time<span class="token punctuation">.</span>Time<span class="token punctuation">,</span> gzipCompression <span class="token builtin">bool</span><span class="token punctuation">)</span> <span class="token builtin">error</span>

    <span class="token comment">// ServeFile提供一个文件（例如发送一个文件，一个zip到客户端你应该使用`SendFile`）</span>
    <span class="token comment">//接收两个参数</span>
    <span class="token comment">//文件名/路径（字符串）</span>
    <span class="token comment">// gzipCompression（bool）</span>

    <span class="token comment">//在此函数调用之前，您可以使用`context＃ContentType`定义自己的“Content-Type”。</span>
    <span class="token comment">//</span>
    <span class="token comment">//此函数不支持恢复（按范围），</span>
    <span class="token comment">//使用ctx.SendFile或路由器的`StaticWeb`代替。</span>

    <span class="token comment">//当您想要向客户端提供动态文件时使用它。</span>
    <span class="token function">ServeFile</span><span class="token punctuation">(</span>filename <span class="token builtin">string</span><span class="token punctuation">,</span> gzipCompression <span class="token builtin">bool</span><span class="token punctuation">)</span> <span class="token builtin">error</span>

    <span class="token comment">// SendFile将文件强制下载发送到客户端</span>
    <span class="token comment">//使用此代替ServeFile来“强制下载”更大的文件到客户端。</span>
    <span class="token function">SendFile</span><span class="token punctuation">(</span>filename <span class="token builtin">string</span><span class="token punctuation">,</span> destinationName <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Cookies                                                    |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// SetCookie添加了一个cookie</span>
    <span class="token function">SetCookie</span><span class="token punctuation">(</span>cookie <span class="token operator">*</span>http<span class="token punctuation">.</span>Cookie<span class="token punctuation">)</span>

    <span class="token comment">// SetCookieKV添加一个cookie，只接收一个名字（字符串）和一个值（字符串）</span>

    <span class="token comment">//如果您使用此方法，它将在2小时后到期</span>
    <span class="token comment">//如果要更改更多字段，请使用ctx.SetCookie或http.SetCookie。</span>
    <span class="token function">SetCookieKV</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> value <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// GetCookie按名称返回cookie的值</span>
    <span class="token comment">//如果没有找到，则返回空字符串。</span>
    <span class="token function">GetCookie</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
    <span class="token comment">// RemoveCookie按名称删除cookie。</span>
    <span class="token function">RemoveCookie</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// VisitAllCookies接受一个循环的访问者</span>
    <span class="token comment">//在每个（请求的）cookie的名称和值上。</span>
    <span class="token function">VisitAllCookies</span><span class="token punctuation">(</span>visitor <span class="token keyword">func</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> value <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token comment">// MaxAge返回“缓存控制”请求标头的值</span>
    <span class="token comment">//秒为int64</span>
    <span class="token comment">//如果未找到标头或解析失败，则返回-1。</span>
    <span class="token function">MaxAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int64</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">//  | Advanced: Response Recorder and Transactions               |</span>
    <span class="token comment">//  +------------------------------------------------------------+</span>
    <span class="token comment">// Record将上下文的基本和直接responseWriter转换为ResponseRecorder</span>
    <span class="token comment">//可用于重置body，重置标头，获取body，</span>
    <span class="token comment">//随时随地获取和设置状态代码。</span>
    <span class="token function">Record</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// Recorder返回上下文的ResponseRecorder</span>
    <span class="token comment">//如果没有录制，则开始录制并返回新上下文的ResponseRecorder</span>
    <span class="token function">Recorder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span>ResponseRecorder

    <span class="token comment">// IsRecording返回响应记录器和真值</span>
    <span class="token comment">//当响应作者正在记录状态代码，正文，标题等时，</span>
    <span class="token comment">// else返回nil和false。</span>
    <span class="token function">IsRecording</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token operator">*</span>ResponseRecorder<span class="token punctuation">,</span> <span class="token builtin">bool</span><span class="token punctuation">)</span>

    <span class="token operator">/</span> BeginTransaction启动范围内的事务。
    <span class="token comment">//您可以搜索有关业务交易如何运作的第三方文章或书籍（这很简单，尤其是在这里）。</span>
    <span class="token comment">//请注意，这是独一无二的</span>
    <span class="token comment">//（=我在Golang上从未见过关于此主题的任何其他示例或代码，到目前为止，与大多数虹膜功能一样......）</span>
    <span class="token comment">//它不包括所有路径</span>
    <span class="token comment">//例如数据库，这应该由用于建立数据库连接的库来管理，</span>
    <span class="token comment">//此事务范围仅用于上下文的响应。</span>
    <span class="token comment">//交易也有自己的中间件生态系统，看看iris.go：UseTransaction。</span>

    <span class="token comment">// 参考 https://github.com/kataras/iris/tree/master/_examples/ for more</span>
    <span class="token function">BeginTransaction</span><span class="token punctuation">(</span>pipe <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>Transaction<span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token comment">//如果调用SkipTransactions，则跳过其余的事务</span>
    <span class="token comment">//如果在第一次交易之前调用，则为全部</span>
    <span class="token function">SkipTransactions</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">//如果事务被跳过或取消，则TransactionsSkipped返回true。</span>
    <span class="token function">TransactionsSkipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// Exec调用`context / Application #ServeCtx`</span>
    <span class="token comment">//基于此上下文但具有更改的方法和路径</span>
    <span class="token comment">//喜欢它是用户请求的，但事实并非如此。</span>
    <span class="token comment">//</span>
    <span class="token comment">//离线表示路由已注册到虹膜，并具有正常路由所具有的所有功能</span>
    <span class="token comment">//但它不能通过浏览获得，它的处理程序仅在其他处理程序的上下文调用它们时执行</span>
    <span class="token comment">//它可以验证路径，有会话，路径参数等等。</span>

    <span class="token comment">//你可以通过app.GetRoute找到路线（“theRouteName”）</span>
    <span class="token comment">//您可以将路径名称设置为：myRoute：= app.Get（“/ mypath”，handler）（“theRouteName”）</span>
    <span class="token comment">//将为路由设置名称并返回其RouteInfo实例以供进一步使用。</span>

    <span class="token comment">//它不会更改全局状态，如果路由处于“离线状态”，它将保持脱机状态。</span>
    <span class="token comment">// app.None（...）和app.GetRoutes（）。离线（路线）/。在线（路线，方法）</span>
    <span class="token comment">//示例：https：//github.com/kataras/iris/tree/master/_examples/routing/route-state</span>

    <span class="token comment">//用户可以通过简单的使用rec：= ctx.Recorder（）获得响应; rec.Body（）/ rec.StatusCode（）/ rec.Header（）。</span>
    <span class="token comment">//保留Context的值和Session，以便能够通过结果路由进行通信。</span>

    <span class="token comment">//这是针对极端用例的，99％的情况永远不会对你有用。</span>
    <span class="token function">Exec</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> path <span class="token builtin">string</span><span class="token punctuation">)</span>

    <span class="token comment">// RouteExists报告特定路由是否存在</span>
    <span class="token comment">//如果不在根域内 ，它将从上下文主机的当前子域搜索。</span>
    <span class="token function">RouteExists</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> path <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>

    <span class="token comment">// Application返回属于此上下文的iris app实例。</span>
    <span class="token comment">//值得注意的是这个函数返回一个接口</span>
    <span class="token comment">//应用程序，包含安全的方法</span>
    <span class="token comment">//在服务时间执行。完整的应用程序的字段</span>
    <span class="token comment">//这里没有方法可供开发人员使用。</span>
    <span class="token function">Application</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Application

    <span class="token comment">// String返回此请求的字符串表示形式。</span>
    <span class="token comment">//每个上下文都有一个唯一的字符串表示</span>
    <span class="token comment">//它可以用于简单的调试场景，即打印上下文为字符串。</span>
    <span class="token comment">//它返回什么？一个声明长度的数字</span>
    <span class="token comment">//跟随每个可执行应用程序的总`String`调用</span>
    <span class="token comment">//通过远程IP（客户端），最后是方法：url。</span>
    <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br><span class="line-number">121</span><br><span class="line-number">122</span><br><span class="line-number">123</span><br><span class="line-number">124</span><br><span class="line-number">125</span><br><span class="line-number">126</span><br><span class="line-number">127</span><br><span class="line-number">128</span><br><span class="line-number">129</span><br><span class="line-number">130</span><br><span class="line-number">131</span><br><span class="line-number">132</span><br><span class="line-number">133</span><br><span class="line-number">134</span><br><span class="line-number">135</span><br><span class="line-number">136</span><br><span class="line-number">137</span><br><span class="line-number">138</span><br><span class="line-number">139</span><br><span class="line-number">140</span><br><span class="line-number">141</span><br><span class="line-number">142</span><br><span class="line-number">143</span><br><span class="line-number">144</span><br><span class="line-number">145</span><br><span class="line-number">146</span><br><span class="line-number">147</span><br><span class="line-number">148</span><br><span class="line-number">149</span><br><span class="line-number">150</span><br><span class="line-number">151</span><br><span class="line-number">152</span><br><span class="line-number">153</span><br><span class="line-number">154</span><br><span class="line-number">155</span><br><span class="line-number">156</span><br><span class="line-number">157</span><br><span class="line-number">158</span><br><span class="line-number">159</span><br><span class="line-number">160</span><br><span class="line-number">161</span><br><span class="line-number">162</span><br><span class="line-number">163</span><br><span class="line-number">164</span><br><span class="line-number">165</span><br><span class="line-number">166</span><br><span class="line-number">167</span><br><span class="line-number">168</span><br><span class="line-number">169</span><br><span class="line-number">170</span><br><span class="line-number">171</span><br><span class="line-number">172</span><br><span class="line-number">173</span><br><span class="line-number">174</span><br><span class="line-number">175</span><br><span class="line-number">176</span><br><span class="line-number">177</span><br><span class="line-number">178</span><br><span class="line-number">179</span><br><span class="line-number">180</span><br><span class="line-number">181</span><br><span class="line-number">182</span><br><span class="line-number">183</span><br><span class="line-number">184</span><br><span class="line-number">185</span><br><span class="line-number">186</span><br><span class="line-number">187</span><br><span class="line-number">188</span><br><span class="line-number">189</span><br><span class="line-number">190</span><br><span class="line-number">191</span><br><span class="line-number">192</span><br><span class="line-number">193</span><br><span class="line-number">194</span><br><span class="line-number">195</span><br><span class="line-number">196</span><br><span class="line-number">197</span><br><span class="line-number">198</span><br><span class="line-number">199</span><br><span class="line-number">200</span><br><span class="line-number">201</span><br><span class="line-number">202</span><br><span class="line-number">203</span><br><span class="line-number">204</span><br><span class="line-number">205</span><br><span class="line-number">206</span><br><span class="line-number">207</span><br><span class="line-number">208</span><br><span class="line-number">209</span><br><span class="line-number">210</span><br><span class="line-number">211</span><br><span class="line-number">212</span><br><span class="line-number">213</span><br><span class="line-number">214</span><br><span class="line-number">215</span><br><span class="line-number">216</span><br><span class="line-number">217</span><br><span class="line-number">218</span><br><span class="line-number">219</span><br><span class="line-number">220</span><br><span class="line-number">221</span><br><span class="line-number">222</span><br><span class="line-number">223</span><br><span class="line-number">224</span><br><span class="line-number">225</span><br><span class="line-number">226</span><br><span class="line-number">227</span><br><span class="line-number">228</span><br><span class="line-number">229</span><br><span class="line-number">230</span><br><span class="line-number">231</span><br><span class="line-number">232</span><br><span class="line-number">233</span><br><span class="line-number">234</span><br><span class="line-number">235</span><br><span class="line-number">236</span><br><span class="line-number">237</span><br><span class="line-number">238</span><br><span class="line-number">239</span><br><span class="line-number">240</span><br><span class="line-number">241</span><br><span class="line-number">242</span><br><span class="line-number">243</span><br><span class="line-number">244</span><br><span class="line-number">245</span><br><span class="line-number">246</span><br><span class="line-number">247</span><br><span class="line-number">248</span><br><span class="line-number">249</span><br><span class="line-number">250</span><br><span class="line-number">251</span><br><span class="line-number">252</span><br><span class="line-number">253</span><br><span class="line-number">254</span><br><span class="line-number">255</span><br><span class="line-number">256</span><br><span class="line-number">257</span><br><span class="line-number">258</span><br><span class="line-number">259</span><br><span class="line-number">260</span><br><span class="line-number">261</span><br><span class="line-number">262</span><br><span class="line-number">263</span><br><span class="line-number">264</span><br><span class="line-number">265</span><br><span class="line-number">266</span><br><span class="line-number">267</span><br><span class="line-number">268</span><br><span class="line-number">269</span><br><span class="line-number">270</span><br><span class="line-number">271</span><br><span class="line-number">272</span><br><span class="line-number">273</span><br><span class="line-number">274</span><br><span class="line-number">275</span><br><span class="line-number">276</span><br><span class="line-number">277</span><br><span class="line-number">278</span><br><span class="line-number">279</span><br><span class="line-number">280</span><br><span class="line-number">281</span><br><span class="line-number">282</span><br><span class="line-number">283</span><br><span class="line-number">284</span><br><span class="line-number">285</span><br><span class="line-number">286</span><br><span class="line-number">287</span><br><span class="line-number">288</span><br><span class="line-number">289</span><br><span class="line-number">290</span><br><span class="line-number">291</span><br><span class="line-number">292</span><br><span class="line-number">293</span><br><span class="line-number">294</span><br><span class="line-number">295</span><br><span class="line-number">296</span><br><span class="line-number">297</span><br><span class="line-number">298</span><br><span class="line-number">299</span><br><span class="line-number">300</span><br><span class="line-number">301</span><br><span class="line-number">302</span><br><span class="line-number">303</span><br><span class="line-number">304</span><br><span class="line-number">305</span><br><span class="line-number">306</span><br><span class="line-number">307</span><br><span class="line-number">308</span><br><span class="line-number">309</span><br><span class="line-number">310</span><br><span class="line-number">311</span><br><span class="line-number">312</span><br><span class="line-number">313</span><br><span class="line-number">314</span><br><span class="line-number">315</span><br><span class="line-number">316</span><br><span class="line-number">317</span><br><span class="line-number">318</span><br><span class="line-number">319</span><br><span class="line-number">320</span><br><span class="line-number">321</span><br><span class="line-number">322</span><br><span class="line-number">323</span><br><span class="line-number">324</span><br><span class="line-number">325</span><br><span class="line-number">326</span><br><span class="line-number">327</span><br><span class="line-number">328</span><br><span class="line-number">329</span><br><span class="line-number">330</span><br><span class="line-number">331</span><br><span class="line-number">332</span><br><span class="line-number">333</span><br><span class="line-number">334</span><br><span class="line-number">335</span><br><span class="line-number">336</span><br><span class="line-number">337</span><br><span class="line-number">338</span><br><span class="line-number">339</span><br><span class="line-number">340</span><br><span class="line-number">341</span><br><span class="line-number">342</span><br><span class="line-number">343</span><br><span class="line-number">344</span><br><span class="line-number">345</span><br><span class="line-number">346</span><br><span class="line-number">347</span><br><span class="line-number">348</span><br><span class="line-number">349</span><br><span class="line-number">350</span><br><span class="line-number">351</span><br><span class="line-number">352</span><br><span class="line-number">353</span><br><span class="line-number">354</span><br><span class="line-number">355</span><br><span class="line-number">356</span><br><span class="line-number">357</span><br><span class="line-number">358</span><br><span class="line-number">359</span><br><span class="line-number">360</span><br><span class="line-number">361</span><br><span class="line-number">362</span><br><span class="line-number">363</span><br><span class="line-number">364</span><br><span class="line-number">365</span><br><span class="line-number">366</span><br><span class="line-number">367</span><br><span class="line-number">368</span><br><span class="line-number">369</span><br><span class="line-number">370</span><br><span class="line-number">371</span><br><span class="line-number">372</span><br><span class="line-number">373</span><br><span class="line-number">374</span><br><span class="line-number">375</span><br><span class="line-number">376</span><br><span class="line-number">377</span><br><span class="line-number">378</span><br><span class="line-number">379</span><br><span class="line-number">380</span><br><span class="line-number">381</span><br><span class="line-number">382</span><br><span class="line-number">383</span><br><span class="line-number">384</span><br><span class="line-number">385</span><br><span class="line-number">386</span><br><span class="line-number">387</span><br><span class="line-number">388</span><br><span class="line-number">389</span><br><span class="line-number">390</span><br><span class="line-number">391</span><br><span class="line-number">392</span><br><span class="line-number">393</span><br><span class="line-number">394</span><br><span class="line-number">395</span><br><span class="line-number">396</span><br><span class="line-number">397</span><br><span class="line-number">398</span><br><span class="line-number">399</span><br><span class="line-number">400</span><br><span class="line-number">401</span><br><span class="line-number">402</span><br><span class="line-number">403</span><br><span class="line-number">404</span><br><span class="line-number">405</span><br><span class="line-number">406</span><br><span class="line-number">407</span><br><span class="line-number">408</span><br><span class="line-number">409</span><br><span class="line-number">410</span><br><span class="line-number">411</span><br><span class="line-number">412</span><br><span class="line-number">413</span><br><span class="line-number">414</span><br><span class="line-number">415</span><br><span class="line-number">416</span><br><span class="line-number">417</span><br><span class="line-number">418</span><br><span class="line-number">419</span><br><span class="line-number">420</span><br><span class="line-number">421</span><br><span class="line-number">422</span><br><span class="line-number">423</span><br><span class="line-number">424</span><br><span class="line-number">425</span><br><span class="line-number">426</span><br><span class="line-number">427</span><br><span class="line-number">428</span><br><span class="line-number">429</span><br><span class="line-number">430</span><br><span class="line-number">431</span><br><span class="line-number">432</span><br><span class="line-number">433</span><br><span class="line-number">434</span><br><span class="line-number">435</span><br><span class="line-number">436</span><br><span class="line-number">437</span><br><span class="line-number">438</span><br><span class="line-number">439</span><br><span class="line-number">440</span><br><span class="line-number">441</span><br><span class="line-number">442</span><br><span class="line-number">443</span><br><span class="line-number">444</span><br><span class="line-number">445</span><br><span class="line-number">446</span><br><span class="line-number">447</span><br><span class="line-number">448</span><br><span class="line-number">449</span><br><span class="line-number">450</span><br><span class="line-number">451</span><br><span class="line-number">452</span><br><span class="line-number">453</span><br><span class="line-number">454</span><br><span class="line-number">455</span><br><span class="line-number">456</span><br><span class="line-number">457</span><br><span class="line-number">458</span><br><span class="line-number">459</span><br><span class="line-number">460</span><br><span class="line-number">461</span><br><span class="line-number">462</span><br><span class="line-number">463</span><br><span class="line-number">464</span><br><span class="line-number">465</span><br><span class="line-number">466</span><br><span class="line-number">467</span><br><span class="line-number">468</span><br><span class="line-number">469</span><br><span class="line-number">470</span><br><span class="line-number">471</span><br><span class="line-number">472</span><br><span class="line-number">473</span><br><span class="line-number">474</span><br><span class="line-number">475</span><br><span class="line-number">476</span><br><span class="line-number">477</span><br><span class="line-number">478</span><br><span class="line-number">479</span><br><span class="line-number">480</span><br><span class="line-number">481</span><br><span class="line-number">482</span><br><span class="line-number">483</span><br><span class="line-number">484</span><br><span class="line-number">485</span><br><span class="line-number">486</span><br><span class="line-number">487</span><br><span class="line-number">488</span><br><span class="line-number">489</span><br><span class="line-number">490</span><br><span class="line-number">491</span><br><span class="line-number">492</span><br><span class="line-number">493</span><br><span class="line-number">494</span><br><span class="line-number">495</span><br><span class="line-number">496</span><br><span class="line-number">497</span><br><span class="line-number">498</span><br><span class="line-number">499</span><br><span class="line-number">500</span><br><span class="line-number">501</span><br><span class="line-number">502</span><br><span class="line-number">503</span><br><span class="line-number">504</span><br><span class="line-number">505</span><br><span class="line-number">506</span><br><span class="line-number">507</span><br><span class="line-number">508</span><br><span class="line-number">509</span><br><span class="line-number">510</span><br><span class="line-number">511</span><br><span class="line-number">512</span><br><span class="line-number">513</span><br><span class="line-number">514</span><br><span class="line-number">515</span><br><span class="line-number">516</span><br><span class="line-number">517</span><br><span class="line-number">518</span><br><span class="line-number">519</span><br><span class="line-number">520</span><br><span class="line-number">521</span><br><span class="line-number">522</span><br><span class="line-number">523</span><br><span class="line-number">524</span><br><span class="line-number">525</span><br><span class="line-number">526</span><br><span class="line-number">527</span><br><span class="line-number">528</span><br><span class="line-number">529</span><br><span class="line-number">530</span><br><span class="line-number">531</span><br><span class="line-number">532</span><br><span class="line-number">533</span><br><span class="line-number">534</span><br><span class="line-number">535</span><br><span class="line-number">536</span><br><span class="line-number">537</span><br><span class="line-number">538</span><br><span class="line-number">539</span><br><span class="line-number">540</span><br><span class="line-number">541</span><br><span class="line-number">542</span><br><span class="line-number">543</span><br><span class="line-number">544</span><br><span class="line-number">545</span><br><span class="line-number">546</span><br><span class="line-number">547</span><br><span class="line-number">548</span><br><span class="line-number">549</span><br><span class="line-number">550</span><br><span class="line-number">551</span><br><span class="line-number">552</span><br><span class="line-number">553</span><br><span class="line-number">554</span><br><span class="line-number">555</span><br><span class="line-number">556</span><br><span class="line-number">557</span><br><span class="line-number">558</span><br><span class="line-number">559</span><br><span class="line-number">560</span><br><span class="line-number">561</span><br><span class="line-number">562</span><br><span class="line-number">563</span><br><span class="line-number">564</span><br><span class="line-number">565</span><br><span class="line-number">566</span><br><span class="line-number">567</span><br><span class="line-number">568</span><br><span class="line-number">569</span><br><span class="line-number">570</span><br><span class="line-number">571</span><br><span class="line-number">572</span><br><span class="line-number">573</span><br><span class="line-number">574</span><br><span class="line-number">575</span><br><span class="line-number">576</span><br><span class="line-number">577</span><br><span class="line-number">578</span><br><span class="line-number">579</span><br><span class="line-number">580</span><br><span class="line-number">581</span><br><span class="line-number">582</span><br><span class="line-number">583</span><br><span class="line-number">584</span><br><span class="line-number">585</span><br><span class="line-number">586</span><br><span class="line-number">587</span><br><span class="line-number">588</span><br><span class="line-number">589</span><br><span class="line-number">590</span><br><span class="line-number">591</span><br><span class="line-number">592</span><br><span class="line-number">593</span><br><span class="line-number">594</span><br><span class="line-number">595</span><br><span class="line-number">596</span><br><span class="line-number">597</span><br><span class="line-number">598</span><br><span class="line-number">599</span><br><span class="line-number">600</span><br><span class="line-number">601</span><br><span class="line-number">602</span><br><span class="line-number">603</span><br><span class="line-number">604</span><br><span class="line-number">605</span><br><span class="line-number">606</span><br><span class="line-number">607</span><br><span class="line-number">608</span><br><span class="line-number">609</span><br><span class="line-number">610</span><br><span class="line-number">611</span><br><span class="line-number">612</span><br><span class="line-number">613</span><br><span class="line-number">614</span><br><span class="line-number">615</span><br><span class="line-number">616</span><br><span class="line-number">617</span><br><span class="line-number">618</span><br><span class="line-number">619</span><br><span class="line-number">620</span><br><span class="line-number">621</span><br><span class="line-number">622</span><br><span class="line-number">623</span><br><span class="line-number">624</span><br><span class="line-number">625</span><br><span class="line-number">626</span><br><span class="line-number">627</span><br><span class="line-number">628</span><br><span class="line-number">629</span><br><span class="line-number">630</span><br><span class="line-number">631</span><br><span class="line-number">632</span><br><span class="line-number">633</span><br><span class="line-number">634</span><br><span class="line-number">635</span><br><span class="line-number">636</span><br><span class="line-number">637</span><br><span class="line-number">638</span><br><span class="line-number">639</span><br><span class="line-number">640</span><br><span class="line-number">641</span><br><span class="line-number">642</span><br><span class="line-number">643</span><br><span class="line-number">644</span><br><span class="line-number">645</span><br><span class="line-number">646</span><br><span class="line-number">647</span><br><span class="line-number">648</span><br><span class="line-number">649</span><br><span class="line-number">650</span><br><span class="line-number">651</span><br><span class="line-number">652</span><br><span class="line-number">653</span><br><span class="line-number">654</span><br><span class="line-number">655</span><br><span class="line-number">656</span><br><span class="line-number">657</span><br><span class="line-number">658</span><br><span class="line-number">659</span><br><span class="line-number">660</span><br><span class="line-number">661</span><br><span class="line-number">662</span><br><span class="line-number">663</span><br><span class="line-number">664</span><br><span class="line-number">665</span><br><span class="line-number">666</span><br><span class="line-number">667</span><br><span class="line-number">668</span><br><span class="line-number">669</span><br><span class="line-number">670</span><br><span class="line-number">671</span><br><span class="line-number">672</span><br><span class="line-number">673</span><br><span class="line-number">674</span><br><span class="line-number">675</span><br><span class="line-number">676</span><br><span class="line-number">677</span><br><span class="line-number">678</span><br><span class="line-number">679</span><br><span class="line-number">680</span><br><span class="line-number">681</span><br><span class="line-number">682</span><br><span class="line-number">683</span><br><span class="line-number">684</span><br><span class="line-number">685</span><br><span class="line-number">686</span><br><span class="line-number">687</span><br><span class="line-number">688</span><br><span class="line-number">689</span><br><span class="line-number">690</span><br><span class="line-number">691</span><br><span class="line-number">692</span><br><span class="line-number">693</span><br><span class="line-number">694</span><br><span class="line-number">695</span><br><span class="line-number">696</span><br><span class="line-number">697</span><br><span class="line-number">698</span><br><span class="line-number">699</span><br><span class="line-number">700</span><br><span class="line-number">701</span><br><span class="line-number">702</span><br><span class="line-number">703</span><br><span class="line-number">704</span><br><span class="line-number">705</span><br><span class="line-number">706</span><br><span class="line-number">707</span><br><span class="line-number">708</span><br><span class="line-number">709</span><br><span class="line-number">710</span><br><span class="line-number">711</span><br><span class="line-number">712</span><br><span class="line-number">713</span><br><span class="line-number">714</span><br><span class="line-number">715</span><br><span class="line-number">716</span><br><span class="line-number">717</span><br><span class="line-number">718</span><br><span class="line-number">719</span><br><span class="line-number">720</span><br><span class="line-number">721</span><br><span class="line-number">722</span><br><span class="line-number">723</span><br><span class="line-number">724</span><br><span class="line-number">725</span><br><span class="line-number">726</span><br><span class="line-number">727</span><br><span class="line-number">728</span><br><span class="line-number">729</span><br><span class="line-number">730</span><br><span class="line-number">731</span><br><span class="line-number">732</span><br><span class="line-number">733</span><br><span class="line-number">734</span><br><span class="line-number">735</span><br><span class="line-number">736</span><br><span class="line-number">737</span><br><span class="line-number">738</span><br><span class="line-number">739</span><br><span class="line-number">740</span><br><span class="line-number">741</span><br><span class="line-number">742</span><br><span class="line-number">743</span><br><span class="line-number">744</span><br><span class="line-number">745</span><br><span class="line-number">746</span><br><span class="line-number">747</span><br><span class="line-number">748</span><br><span class="line-number">749</span><br><span class="line-number">750</span><br><span class="line-number">751</span><br><span class="line-number">752</span><br><span class="line-number">753</span><br><span class="line-number">754</span><br></div></div><h2 id="_3-普通路由"><a href="#_3-普通路由" class="header-anchor">#</a> 3 普通路由</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>

<span class="token comment">// 处理函数</span>
<span class="token keyword">func</span> <span class="token function">handler</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	ctx<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;method:%s path:%s&quot;</span><span class="token punctuation">,</span> ctx<span class="token punctuation">.</span><span class="token function">Method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> ctx<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">//剩下做对应的操作，这里省略</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//GET 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">// POST 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">// PUT 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Put</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">// DELETE 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//OPTIONS 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Options</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//TRACE 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Trace</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//CONNECT 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Connect</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//HEAD 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Head</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">// PATCH 方法</span>
	app<span class="token punctuation">.</span><span class="token function">Patch</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//任意的http请求方法如option等</span>
	app<span class="token punctuation">.</span><span class="token function">Any</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	<span class="token comment">//8085 监听端口 ，如果未发现服务则返回系统自定义的错误</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8085&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithoutServerError</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>ErrServerClosed<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="_4-动态路由"><a href="#_4-动态路由" class="header-anchor">#</a> 4 动态路由</h2> <div class="custom-block tip"><p class="custom-block-title">概念</p> <p>Iris框架在进行处理<code>http</code>请求时,可对请求路径进行动态路由配置,即支持请求<code>url</code>中包含正则表达式。 正则表达式的具体规则为：</p> <ul><li>1、使用<code>{}</code>对正则表达式进行包裹，<code>url</code>中出现类似<code>{}</code>样式的格式，即识别为正则表达式</li> <li>2、支持自定义正则表达式的变量的命名，变量名用字母表示。比如：<code>{name}</code></li> <li>3、支持对自定义正则表达式变量的数据类型限制，变量名和对应的数据类型之间用<code>“:”</code>分隔开。比如：<code>{name:string}</code>表示正则表达式为<code>name</code>，类型限定为<code>string</code>类型</li> <li>4、通过<code>context.Params()</code>的<code>Get()</code>和<code>GetXxx()</code>系列方法来获取对应的请求<code>url</code>中的正则表达式的变量</li> <li>5、正则表达式支持变量的数据类型包括：<code>string、int、uint、bool</code>等</li></ul></div> <h3 id="_4-1-举例"><a href="#_4-1-举例" class="header-anchor">#</a> 4.1 举例</h3> <blockquote><p>下面示例包含路由传参、设置可变参数类型、设置错误码、正则表达式等举例，更多示例<a href="https://github.com/kataras/iris/tree/master/_examples" target="_blank" rel="noopener noreferrer">请参考源码<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// 路由传参</span>
	<span class="token comment">//请求路径参数正则匹配 /username/123    /username/ABC等等</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/username/{name}&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//获取对应动态参数的值</span>
		name <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// 设置可变参数类型</span>
	<span class="token comment">//请求路径的参数类型必须为布尔类型即true false</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/api/users/{isLogin:bool}&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		isLogin<span class="token punctuation">,</span> err <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetBool</span><span class="token punctuation">(</span><span class="token string">&quot;isLogin&quot;</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			context<span class="token punctuation">.</span><span class="token function">StatusCode</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span>StatusNonAuthoritativeInfo<span class="token punctuation">)</span>
			<span class="token keyword">return</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">if</span> isLogin <span class="token punctuation">{</span>
			context<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">&quot; 已登录 &quot;</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
			context<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span><span class="token string">&quot; 未登录 &quot;</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// 设置错误码</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/profile/{id:int min(1)}/friends/{friendId:int max(8) else 504}&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//获取可变参数的数据</span>
		i<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span>
		getInt<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> context<span class="token punctuation">.</span><span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetInt</span><span class="token punctuation">(</span><span class="token string">&quot;friendId&quot;</span><span class="token punctuation">)</span>
		context<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;Hello id:%d looking for friend id: &quot;</span><span class="token punctuation">,</span> i<span class="token punctuation">,</span> getInt<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 如果没有传递所有路由的macros，这将抛出504错误代码而不是404.</span>

	<span class="token comment">// 正则表达式,即name可变参数只匹配小写的字母</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/lowercase/{name:string regexp(^[a-z]+)}&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		context<span class="token punctuation">.</span><span class="token function">Writef</span><span class="token punctuation">(</span><span class="token string">&quot;name should be only lowercase, otherwise this handler will never executed: %s&quot;</span><span class="token punctuation">,</span> context<span class="token punctuation">.</span><span class="token function">Params</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token comment">//8085 监听端口,并设置编码格式为UTF-8</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8085&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithCharset</span><span class="token punctuation">(</span><span class="token string">&quot;UTF-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br></div></div><h2 id="_5-路由分组"><a href="#_5-路由分组" class="header-anchor">#</a> 5 路由分组</h2> <blockquote><p>一组路由可以用前缀路径分组,组之间共享相同的中间件和模板布局,组内可以嵌套组。
<code>.Party</code>被用于分组路由,开发者可以声明不限数量的分组。</p></blockquote> <h3 id="_5-1-路由组示例"><a href="#_5-1-路由组示例" class="header-anchor">#</a> 5.1 路由组示例</h3> <blockquote><p>常见分组是按照业务模块进行分组的,比如用户模块（包含登录、注册、获取用户信息等),详细请看如下示例</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">//用户模块users分组说明，这样一个分组包含用户的相关操作</span>
	<span class="token comment">// xxx/users/login 登录</span>
	<span class="token comment">// xxx/users/register 注册</span>
	<span class="token comment">// xxx/users/info 获取用户信息</span>

	<span class="token comment">/**
	* 路由组请求定义，根据相同前缀users分组
	 */</span>
	userParty <span class="token operator">:=</span> app<span class="token punctuation">.</span><span class="token function">Party</span><span class="token punctuation">(</span><span class="token string">&quot;/users&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//这里不做任何操作，转到下一级请求去处理</span>
		context<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">/**
	* 如果在路由组下面的下一级请求调用了context.Next()
	* 则在完成请求后会执行这个方法，比如下面的用户登录
	 */</span>
	userParty<span class="token punctuation">.</span><span class="token function">Done</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//请求完成后打印对应的日志</span>
		context<span class="token punctuation">.</span><span class="token function">Application</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Infof</span><span class="token punctuation">(</span><span class="token string">&quot;response sent to &quot;</span> <span class="token operator">+</span> context<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">/**
	* 路由组下面的下一级请求 用户登录
	* .../users/login
	 */</span>
	userParty<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/login&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;用户登录功能&quot;</span><span class="token punctuation">)</span>
		context<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;用户登录功能&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
		<span class="token comment">//手动显示调用路由组的Done方法</span>
		context<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">/**
	* 路由组下面的下一级请求 用户注册
	* .../users/register
	 */</span>
	userParty<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/register&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;用户注册功能&quot;</span><span class="token punctuation">)</span>
		context<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;用户注册功能&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">/**
	* 路由组下面的下一级请求  获取用户信息
	* .../users/info
	 */</span>
	userParty<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/info&quot;</span><span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>context iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		app<span class="token punctuation">.</span><span class="token function">Logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Info</span><span class="token punctuation">(</span><span class="token string">&quot;获取用户信息功能&quot;</span><span class="token punctuation">)</span>
		context<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;获取用户信息&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//8085 监听端口,并设置编码格式为UTF-8</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8085&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> iris<span class="token punctuation">.</span><span class="token function">WithCharset</span><span class="token punctuation">(</span><span class="token string">&quot;UTF-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><h2 id="_6-路由中间件"><a href="#_6-路由中间件" class="header-anchor">#</a> 6 路由中间件</h2> <blockquote><p>当我们在<code>Iris</code>中讨论中间件时，我们讨论的是在<code>HTTP</code>请求生命周期中在主处理程序代码之前和/或之后运行代码。例如，记录中间件可能会将传入的请求详细信息写入日志，然后在写入有关日志响应的详细信息之前调用处理程序代码。关于中间件的一个很酷的事情是这些单元非常灵活和可重用。</p></blockquote> <h3 id="_6-1-简单示例"><a href="#_6-1-简单示例" class="header-anchor">#</a> 6.1 简单示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建一个app结构体指针对象</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">/*
		   请求localhost:8085/hello 下面函数执行的顺序是
			before--&gt;mainHandler--&gt;after
	*/</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/hello&quot;</span><span class="token punctuation">,</span> before<span class="token punctuation">,</span> mainHandler<span class="token punctuation">,</span> after<span class="token punctuation">)</span>
	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8085&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">before</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	shareInformation <span class="token operator">:=</span> <span class="token string">&quot;this is a sharable information between handlers&quot;</span>
	requestPath <span class="token operator">:=</span> ctx<span class="token punctuation">.</span><span class="token function">Path</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Before the mainHandler: &quot;</span> <span class="token operator">+</span> requestPath<span class="token punctuation">)</span>
	<span class="token comment">//设置一个参数值传递给下一个handler</span>
	ctx<span class="token punctuation">.</span><span class="token function">Values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;info&quot;</span><span class="token punctuation">,</span> shareInformation<span class="token punctuation">)</span>
	ctx<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//继续执行下一个handler，在本例中是mainHandler。</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">after</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;After the mainHandler&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">mainHandler</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Inside mainHandler&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//从“before”处理程序获取信息</span>
	info <span class="token operator">:=</span> ctx<span class="token punctuation">.</span><span class="token function">Values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">&quot;info&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 写些东西给客户端作为回应。</span>
	ctx<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;Response&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	ctx<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;br/&gt; Info: &quot;</span> <span class="token operator">+</span> info<span class="token punctuation">)</span>
	ctx<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 继续下一个handler 这里是after</span>
<span class="token punctuation">}</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>输出结果<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
Before the mainHandler<span class="token punctuation">:</span> <span class="token operator">/</span>hello
Inside mainHandler
After the mainHandler
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h3 id="_6-2-全局中间件"><a href="#_6-2-全局中间件" class="header-anchor">#</a> 6.2 全局中间件</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;github.com/kataras/iris/v12&quot;</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	app <span class="token operator">:=</span> iris<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">//将“before”处理程序注册为将要执行的第一个处理程序</span>
	<span class="token comment">//在所有域的路由上。</span>
	<span class="token comment">//或使用`UseGlobal`注册一个将跨子域触发的中间件。</span>
	app<span class="token punctuation">.</span><span class="token function">Use</span><span class="token punctuation">(</span>before<span class="token punctuation">)</span>

	<span class="token comment">//将“after”处理程序注册为将要执行的最后一个处理程序</span>
	<span class="token comment">//在所有域的路由'处理程序之后。</span>
	app<span class="token punctuation">.</span><span class="token function">Done</span><span class="token punctuation">(</span>after<span class="token punctuation">)</span>

	<span class="token comment">// register our routes.</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> indexHandler<span class="token punctuation">)</span>
	app<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/contact&quot;</span><span class="token punctuation">,</span> contactHandler<span class="token punctuation">)</span>

	app<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>iris<span class="token punctuation">.</span><span class="token function">Addr</span><span class="token punctuation">(</span><span class="token string">&quot;:8080&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">before</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// [...]</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">after</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// [...]</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">indexHandler</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	ctx<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;Index&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	ctx<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 执行通过`Done`注册的“after”处理程序。</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">contactHandler</span><span class="token punctuation">(</span>ctx iris<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// write something to the client as a response.</span>
	ctx<span class="token punctuation">.</span><span class="token function">HTML</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;h1&gt;Contact&lt;/h1&gt;&quot;</span><span class="token punctuation">)</span>
	ctx<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 执行通过`Done`注册的“after”处理程序。</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><h3 id="_6-3-相关源码学习"><a href="#_6-3-相关源码学习" class="header-anchor">#</a> 6.3 相关源码学习</h3> <table><thead><tr><th style="text-align:center;">Middleware名称</th> <th>Example</th></tr></thead> <tbody><tr><td style="text-align:center;">基本认证basic authentication</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/authentication/basicauth" target="_blank" rel="noopener noreferrer">basicauth<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:center;">Google reCAPTCHA</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/miscellaneous/recaptcha" target="_blank" rel="noopener noreferrer">reCAPTCHA<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:center;">本地化和国际化ocalization and internationalization</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n" target="_blank" rel="noopener noreferrer">i81n<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:center;">请求记录器request logger</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/http_request/request-logger" target="_blank" rel="noopener noreferrer">request-logger<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:center;">性能分析 (pprof)</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/miscellaneous/pprof" target="_blank" rel="noopener noreferrer">pprof<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:center;">恢复recover</td> <td><a href="https://github.com/kataras/iris/tree/master/_examples/miscellaneous/recover" target="_blank" rel="noopener noreferrer">recover<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr></tbody></table> <h3 id="_6-4-常用中间件"><a href="#_6-4-常用中间件" class="header-anchor">#</a> 6.4 常用中间件</h3> <table><thead><tr><th style="text-align:left;">Middleware</th> <th>描述</th> <th>Example</th></tr></thead> <tbody><tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/jwt" target="_blank" rel="noopener noreferrer">jwt<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>检查请求的Authorization头,进行JWT检查和解析</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/jwt/_example" target="_blank" rel="noopener noreferrer">jwt_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/cors" target="_blank" rel="noopener noreferrer">cors<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>HTTP 跨域请求。</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/cors/_example" target="_blank" rel="noopener noreferrer">cors_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/secure" target="_blank" rel="noopener noreferrer">secure<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>一些快速安全实现的中间件。</td> <td><a href="https://github.com/iris-contrib/middleware/blob/master/secure/_example/main.go" target="_blank" rel="noopener noreferrer">secure_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/tollboothic" target="_blank" rel="noopener noreferrer">tollbooth<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>用于验证 HTTP 请求速率的通用中间件</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/tollbooth/_examples/limit-handler" target="_blank" rel="noopener noreferrer">tollbooth_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/cloudwatch" target="_blank" rel="noopener noreferrer">cloudwatch<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>AWS cloudwatch 指标中间件。</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/cloudwatch/_example" target="_blank" rel="noopener noreferrer">cloudwatch_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/newrelic" target="_blank" rel="noopener noreferrer">new relic<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>官方 New Relic Go Agent.</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/newrelic/_example" target="_blank" rel="noopener noreferrer">newrelic_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/prometheus" target="_blank" rel="noopener noreferrer">prometheus<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>轻松为 prometheus 检测工具创建指标端点</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/prometheus/_example" target="_blank" rel="noopener noreferrer">prometheus_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr> <tr><td style="text-align:left;"><a href="https://github.com/iris-contrib/middleware/tree/master/casbin" target="_blank" rel="noopener noreferrer">casbin<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td> <td>支持各种权限模型的授权库,例如ACL,RBAC,ABAC</td> <td><a href="https://github.com/iris-contrib/middleware/tree/master/casbin/_examples" target="_blank" rel="noopener noreferrer">casbin_example<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></td></tr></tbody></table></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/web/iris-go/handle-http.html" class="prev">
        处理HTTP请求
      </a></span> <span class="next"><a href="/go/web/iris-go/error-handling.html">
        错误处理
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/102.c7d7ed9c.js" defer></script>
  </body>
</html>
